<template>
  <div class="header">
    <div class="introduce">
      <img src="../assets/image/BUO_Logo.png" class="image" />
      <div class="info">
        <h1 class="title">BUO宠物生活馆</h1>
      </div>
    </div>
    <div class="navigation">
      <p class="nav-item" @click="$router.push('/')">首页</p>
      <p @click="showAbout = true" class="nav-item">关于我们</p>
      <about-us v-if="showAbout" @close="showAbout = false" />
      <p @click="showContact = true" class="nav-item">联系我们</p>
      <contact-us v-if="showContact" @close="showContact = false" />

      <!-- 管理员特有的菜单项 -->
      <p v-if="isAdmin" class="nav-item" @click="$router.push('/backindex')">
        后台管理
      </p>

      <!-- 未登录状态 -->
      <div v-if="!isLoggedIn" class="auth-buttons">
        <a href="#" class="login-btn" @click="showLogin = true">
          <span>登录</span>
        </a>
        <login
          v-if="showLogin"
          @close="showLogin = false"
          @login-success="handleLoginSuccess"
          @register="handleRegisterClick"
        />
        <a href="#" class="register-btn" @click="showRegister = true">
          <span>注册</span>
        </a>
        <register
          v-if="showRegister"
          @close="showRegister = false"
          @login="handleLoginClick"
        />
      </div>

      <!-- 已登录状态 -->
      <div v-else class="user-menu">
        <el-dropdown @command="handleUserCommand">
          <span class="user-name">
            {{ username }}
            <ElIcon size="16"><CaretBottom /></ElIcon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="profile">个人中心</el-dropdown-item>
              <el-dropdown-item command="logout">注销登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
  </div>
</template>

<script setup>
import { useRouter } from "vue-router";
import { ref, onMounted, computed } from "vue";
import aboutUs from "../components/aboutUs.vue";
import contactUs from "../components/contactUs.vue";
import login from "./login.vue";
import register from "./register.vue";
import { ElIcon } from "element-plus";
import { CaretBottom } from "@element-plus/icons-vue";
import { useStore } from "vuex";

const store = useStore();
const showAbout = ref(false);
const showContact = ref(false);
const router = useRouter();
const showLogin = ref(false);
const showRegister = ref(false);

// 从Vuex store获取用户状态
const isLoggedIn = computed(() => store.getters["user/isLoggedIn"]);
const username = computed(() => store.getters["user/username"] || "用户");
const isAdmin = computed(() => store.getters["user/isAdmin"]);

const handleRegisterClick = () => {
  showLogin.value = false;
  showRegister.value = true;
};

const handleLoginClick = () => {
  showRegister.value = false;
  showLogin.value = true;
};

// 登录成功处理
const handleLoginSuccess = () => {
  showLogin.value = false;
  // 用户信息已在login action中更新到store，不需要额外操作
};

const handleUserCommand = (command) => {
  if (command === "profile") {
    router.push("/profile/info");
  } else if (command === "logout") {
    store.dispatch("user/logout").then(() => {
      // 登出后重定向到首页
      router.push("/");
    });
  }
};

onMounted(() => {
  // 初始化时，如果有token但store中没有用户信息，尝试获取用户信息
  const token = localStorage.getItem("token");
  if (token && !isLoggedIn.value) {
    // 调用store的action获取用户信息
    store.dispatch("user/getUserInfo").catch((err) => {
      console.error("获取用户信息失败:", err);
    });
  }
});
</script>

<style lang="less" scoped>
.header {
  background: #fff;
  padding: 10px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 2px solid #e0e0e0;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.06);
}

.introduce {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-left: 20px;
  .image {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    object-fit: contain;
    margin-right: 0;
    background: #fff;
    border: none;
    box-shadow: none;
  }
  .info {
    display: flex;
    align-items: center;
    .title {
      font-size: 28px;
      font-weight: 700;
      color: #8d26e6;
      letter-spacing: 2px;
      margin: 0;
      font-family: "微软雅黑", "Arial", sans-serif;
      display: flex;
      align-items: center;
    }
  }
}

.navigation {
  display: flex;
  align-items: center;
  font-size: 18px;
  margin-right: 60px;
  gap: 24px;
  position: relative;
  .nav-item {
    cursor: pointer;
    color: #333;
    background: transparent;
    border-radius: 8px;
    padding: 0 10px;
    font-weight: 500;
    transition: color 0.2s;
    height: 38px;
    line-height: 38px;
    display: flex;
    align-items: center;
    &:hover,
    &.active {
      color: #8d26e6;
      background: transparent;
    }
  }
  .auth-buttons {
    display: flex;
    align-items: center;
    gap: 10px;
  }
  .login-btn {
    color: #fff;
    background: linear-gradient(90deg, #8d26e6 0%, #a259ff 100%);
    font-weight: 600;
    padding: 8px 24px;
    border-radius: 30px;
    height: 30px;
    line-height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 15px rgba(140, 38, 230, 0.2);
    margin-left: 18px;
    font-size: 18px;
    text-decoration: none;
    transition: all 0.3s ease;
    border: none;
    &:hover {
      background: linear-gradient(90deg, #7a18d6 0%, #9340ff 100%);
      transform: translateY(-2px);
      box-shadow: 0 6px 20px rgba(140, 38, 230, 0.3);
      color: #fff;
    }
  }
  .register-btn {
    color: #8d26e6;
    background: #ffffff;
    font-weight: 600;
    padding: 8px 24px;
    border-radius: 30px;
    height: 30px;
    line-height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 15px rgba(140, 38, 230, 0.1);
    margin-left: 15px;
    font-size: 18px;
    text-decoration: none;
    transition: all 0.3s ease;
    border: 2px solid #f0e0ff;
    &:hover {
      background: #f9f3ff;
      border-color: #e0c0ff;
      transform: translateY(-2px);
      box-shadow: 0 6px 20px rgba(140, 38, 230, 0.2);
      color: #a259ff;
    }
  }

  .user-menu {
    position: relative;
  }

  .user-name {
    color: #333;
    cursor: pointer;
    padding: 5px 10px;
    border-radius: 8px;
    transition: background-color 0.2s;
    font-size: 18px;
    font-weight: 500;
    &:hover {
      background-color: #f5f5f5;
    }
  }

  ::v-deep .el-dropdown-menu {
    z-index: 1001 !important;
    min-width: 120px;
    padding: 5px 0;
    background-color: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  }

  .user-name i {
    margin-left: 5px;
    font-size: 14px;
  }
}

// 响应式设计
@media (max-width: 900px) {
  .header {
    padding: 12px 10px;
  }
  .introduce {
    margin-left: 0;
    .info .title {
      font-size: 20px;
    }
    .image {
      width: 30px;
      height: 30px;
    }
  }
  .navigation {
    font-size: 15px;
    margin-right: 10px;
    gap: 12px;
    .user-menu {
      position: relative;
      display: inline-block;
      margin-left: 20px;
    }
    .user-name {
      color: #333;
      cursor: pointer;
      padding: 5px 10px;
    }
    .login-btn,
    .register-btn {
      font-size: 15px;
      height: 32px;
      line-height: 32px;
      padding: 0 16px;
    }
    .nav-item {
      height: 32px;
      line-height: 32px;
      padding: 0 6px;
    }
  }
}

@media (max-width: 600px) {
  .header {
    flex-direction: column;
    align-items: flex-start;
    padding: 10px 4px;
    border-radius: 0 0 8px 8px;
  }
  .introduce {
    flex-direction: row;
    align-items: center;
    gap: 8px;
    margin-bottom: 8px;
    .info .title {
      font-size: 16px;
    }
    .image {
      width: 24px;
      height: 24px;
    }
  }
  .navigation {
    font-size: 13px;
    margin-right: 0;
    gap: 6px;
    .login-btn,
    .register-btn {
      font-size: 13px;
      height: 26px;
      line-height: 26px;
      padding: 0 10px;
      border-radius: 13px;
    }
    .nav-item {
      height: 26px;
      line-height: 26px;
      padding: 0 2px;
    }
  }
}
</style>
